package gold.digger;

import gold.utils.InputUtil;
import gold.vo.TreeNode;

/**
 * Created by fanzhenyu02 on 2021/12/10.
 * common problem solver template.
 */
public class LC979 {
    public long startExecuteTime = System.currentTimeMillis();


    class Solution {
        int ans = 0;

        public int distributeCoins(TreeNode root) {
            distributeCoinsRecur(root);
            return ans;
        }

        public int distributeCoinsRecur(TreeNode root) {
            if (null == root) return 0;
            int leftSide = distributeCoinsRecur(root.left);
            int rightSide = distributeCoinsRecur(root.right);
            ans += Math.abs(leftSide) + Math.abs(rightSide);
            return root.val - 1 + leftSide + rightSide;
        }
    }

    public void run() {
        Solution solution = new Solution();
//        System.out.println(solution.distributeCoins(TreeNode.createTree("[3,0,0]")));
//        System.out.println(solution.distributeCoins(TreeNode.createTree("[0,3,0]")));
//        System.out.println(solution.distributeCoins(TreeNode.createTree("[1,0,2]")));
        System.out.println(solution.distributeCoins(TreeNode.createTree("[1,0,0,null,3]")));
    }

    public static void main(String[] args) throws Exception {
        LC979 an = new LC979();
        an.run();

        System.out.println("\ncurrent solution total execute time: " + (System.currentTimeMillis() - an.startExecuteTime) + " ms.");
    }
}
